import dayjs from 'dayjs'
// 手机号验证
const validatePhone = (rule, value, callback) => {
  const regPhone =
    /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/
  if (regPhone.test(value)) {
    return callback()
  }
  callback(new Error('手机号码不正确'))
}

// 手机号码和固定电话验证
const validatePhoneOrNum = (rule, value, callback) => {
  const regPhone2 =
    /^1\d{10}$|^(0\d{2,3}-?|\(0\d{2,3}\))?[1-9]\d{4,7}(-\d{1,8})?$/
  if (regPhone2.test(value)) {
    return callback()
  }
  callback(new Error('请输入有效的联系方式'))
}

const validatePhoneOrNumNotRequired = (rule, value, callback) => {
  if (value === '' || value === null || value === undefined) {
    return callback()
  } else {
    const regPhone2 =
      /^1\d{10}$|^(0\d{2,3}-?|\(0\d{2,3}\))?[1-9]\d{4,7}(-\d{1,8})?$/
    if (regPhone2.test(value)) {
      return callback()
    }
    callback(new Error('请输入有效的联系方式'))
  }

}

// 身份证号验证
const validateIdCard = (rule, value, callback) => {
  const regIdCard =
    /^[1-9]\d{5}(18|19|20)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/
  if (regIdCard.test(value)) {
    return callback()
  } else {
    callback(new Error('身份证号码不正确'))
  }
}

// 户籍地址（必须精确到社区级别 value是表单中的registerCommunity 社区编码）
const registerAreaValidator = (rule, value, callback) => {
  if (value) {
    return callback()
  } else {
    callback(new Error('户籍地址必须选到社区'))
  }
}
const checkboxValidate = (rule, value, callback) => {
  if (value && value.length > 0) {
    return callback()
  } else {
    callback(new Error('请选择至少一项'))
  }
}

const dateValidator = (rule,value,callback)=>{
  if (value) {
    return callback()
  } else {
    callback(new Error('请选择riqi'))
  }
}
const specificServiceCodeValidator = (rule, value, callback) => {
  if (value) {
    return callback()
  } else {
    callback(new Error('政府服务对象必选'))
  }
}
const addressErrMsg = {
  1: '请选择省份',
  2: '请选择到市一级',
  3: '请选择到区/县一级',
  4: '请选择到乡镇/街道一级',
  5: '请选择到社区一级'
}
const validateAttach = (rule, value, callback) => {
  if (!value) {
    return callback(new Error('请上传附件'))
  } else {
    return callback()
  }
}

// 校验大于等于0的数字-必填
const validateNum1 = (rule, value, callback) => {
  // const num = /^([1-9]+(\.\d+)?)|([0](\.\d+)?)$/
  const num = /^0\.([1-9]|\d[1-9])$|^[1-9]\d{0,8}\.\d{0,2}$|^[1-9]\d{0,8}$/
  if (num.test(value)) {
    return callback()
  } else {
    callback(new Error('请输入大于等于0且格式正确的数字'))
  }
}
// 校验大于等于0的数字-非必填
const validateNum1NotRequired = (rule, value, callback) => {
  if (value === '' || value === null || value === undefined) {
    return callback()
  } else {
    const num = /^0\.([1-9]|\d[1-9])$|^[1-9]\d{0,8}\.\d{0,2}$|^[1-9]\d{0,8}$/
    if (num.test(value)) {
      return callback()
    } else {
      callback(new Error('请输入大于等于0且格式正确的数字'))
    }
  }

}

const validateCreditCode = (rule, value, callback) => {
  const regCreditCode = /^[0-9A-Za-z]{18}$/
  if (regCreditCode.test(value)) {
    return callback()
  } else {
    callback(new Error('必须为18位数字或字母'))
  }
}

const validateBank = (rule, value, callback) => {
  const regExp = /^[0-9]*$/
  if (regExp.test(value)) {
    return callback()
  } else {
    callback(new Error('卡号必须为数字'))
  }
}

export default {
  registerArea: [
    {
      required: true,
      trigger: ['blur', 'change'],
      validator: registerAreaValidator
    }
  ],
  // len 1,2,3,4,5
  addressSelecte: function (msg, len) {
    return [
      {
        required: true,
        trigger: ['change'],
        validator: (rule, value, callback) => {
          if (value.length < len) {
            if (!msg) {
              callback(new Error(addressErrMsg[len]))
            } else {
              callback(new Error(msg || '请选择123'))
            }
          } else {
            callback()
          }
        }
      }
    ]
  },

  specificServiceCode: [
    {
      required: false,
      trigger: ['change'],
      validator: specificServiceCodeValidator
    }
  ],
  phone: [
    { required: true, message: '请输入' },
    { validator: validatePhone, trigger: 'blur' }
  ],
  mobile: [
    { required: true, message: '请输入' },
    { validator: validatePhone, trigger: 'blur' }
  ],
  phoneOrNum: [
    { required: true, message: '请输入' },
    { validator: validatePhoneOrNum, trigger: 'blur' }
  ],
  phoneOrNumNotRequired: [
    { validator: validatePhoneOrNumNotRequired, trigger: 'blur' }
  ],
  name: [{ required: true, message: '请输入', trigger: 'blur' }],
  select: [{ required: true, message: '请选择', trigger: ['blur', 'change'] }],
  checkbox: [
    {
      required: true,
      trigger: ['blur', 'change'],
      validator: checkboxValidate
    }
  ],
  input: [{ required: true, message: '请输入', trigger: ['blur', 'change'] }],
  inputNumber: [
    { required: true, message: '请输入' },
    {
      type: 'number',
      message: '内容必须为数字值',
      transform: (value) => {
        const newValue = value !== "" ? Number(value) : "";
        return newValue;
      }
    }
  ],
  bank: [
    { require: true, message: '请输入' },
    { validator: validateBank, trigger: ['blur', 'change'] }
  ],
  inputChange: [{ required: true, message: '请输入', trigger: ['change'] }],
  upload: [{ validator: validateAttach, trigger: 'change' }],
  cardTypeCode: [
    { required: true, message: '证件类型不能为空', trigger: ['blur', 'change'] }
  ],
  idCard: [
    { required: true, message: '请输入' },
    { validator: validateIdCard, trigger: 'blur' }
  ],
  birthDay: [
    { required: true, message: '出生日期不能为空', trigger: ['blur', 'change'] }
  ],
  age: [
    { required: true, message: '年龄不能为空' },
    { type: 'number', message: '年龄必须为数字值' }
  ],
  nationCode: [
    { required: true, message: '民族不能为空', trigger: ['blur', 'change'] }
  ],
  sexCode: [
    { required: true, message: '性别不能为空', trigger: ['blur', 'change'] }
  ],
  // 菜单名称
  resourceName: [
    { required: true, message: '菜单名不能为空', trigger: 'blur' }
  ],
  // 菜单代码
  resourceCode: [
    { required: true, message: '菜单代码不能为空', trigger: 'blur' }
  ],
  // 菜单类型
  resourceType: [
    { required: true, message: '菜单类型不能为空', trigger: ['blur', 'change'] }
  ],
  // 菜单路径
  resourcePath: [
    { required: true, message: '菜单路径不能为空', trigger: 'blur' }
  ],
  // 菜单排序
  resourceSort: [
    { required: true, message: '菜单排序不能为空' },
    { type: 'number', message: '排序必须为数字值' }
  ],
  idNumber: function (isIdCard) {
    // 参数 是否是身份证
    return [
      {
        required: true,
        trigger: ['blur', 'change'],
        validator: isIdCard
          ? validateIdCard
          : (rule, value, callback) => {
            if (value) {
              return callback()
            } else {
              callback(new Error('请输入'))
            }
          }
      }
    ]
  },
  sameIdNumber: function (isIdCard, idCard) {
    // 参数 是否是身份证
    return [
      {
        required: true,
        trigger: ['blur', 'change'],
        validator: isIdCard
          ? (rule, value, callback) => {
            console.log(rule, value, callback, idCard);
            const regIdCard =
              /^[1-9]\d{5}(18|19|20)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/
            if (regIdCard.test(value)) {
              if (value === idCard) {
                callback(new Error('证件号码不能相同'))
              }
              return callback()
            } else {
              callback(new Error('身份证号码不正确'))
            }
          }
          : (rule, value, callback) => {
            if (value) {
              if (value === idCard) {
                callback(new Error('证件号码不能相同'))
              }
              return callback()
            } else {
              callback(new Error('请输入'))
            }
          }
      }
    ]
  },

  timeBegin: function (end, msg) {
    return [
      {
        required: true, trigger: ['blur', 'change'], validator: end ? (rule, value, callback) => {
          if (dayjs(end).isBefore(dayjs(value))) {
            callback(new Error(msg || '开始时间不能大于结束时间'))
          } else if (value) {
            return callback()
          } else {
            callback(new Error('请选择'))
          }
        } : (rule, value, callback) => {
          if (value) {
            return callback()
          } else {
            callback(new Error('请选择'))
          }
        }
      },
    ]
  },

  timeEnd: function (begin, msg) {
    return [
      {
        required: true, trigger: ['blur', 'change'], validator: begin ? (rule, value, callback) => {
          if (dayjs(begin).isAfter(dayjs(value))) {
            callback(new Error(msg || '结束时间不能小于开始时间'))
          } else if (value) {
            return callback()
          } else {
            callback(new Error('请选择'))
          }
        } : (rule, value, callback) => {
          if (value) {
            return callback()
          } else {
            callback(new Error('请选择'))
          }
        }
      },
    ]
  },

  // 输入框动态验证规则
  isInputRequire: function (val) {
    return [
      {
        required: true,
        trigger: ['blur'],
        validator: val ? (rule, value, callback) => {
          if (value) {
            return callback()
          } else {
            callback(new Error('请输入'))
          }
        } : (rule, value, callback) => {
          return callback()
        }
      }
    ]
  },
  numberInput: function (min, max) {
    return [
      {
        required: true,
        trigger: ['blur', 'change'],
        validator: (rule, value, callback) => {
          if (!Number.isInteger(value)) {
            callback(new Error('请输入数字值'))
          }

          if (value < min) {
            callback(new Error(`值不能小于${min}`))
          }
          if (max === null) {
            callback()
          }
          if (value > max) {
            callback(new Error(`值不能大于${max}`))
          }
          return callback()
        }
      }
    ]
  },
  // 统一社会信用代码校验
  creditCode: [
    { required: true, message: '请输入' },
    { validator: validateCreditCode, trigger: 'blur' }
  ],
  // 大于等于0的数字校验-必填
  isMoreZeroNum: [
    { required: true, message: '请输入' },
    { validator: validateNum1, trigger: 'blur' }
  ],
  // 大于等于0的数字校验-非必填
  isMoreZeroNumNotRequired: [
    { validator: validateNum1NotRequired, trigger: 'blur' }
  ],

}
